/**
 * KAYO APPLIANCE CHAINS.
 * Copyright (c) 2016-2016 All Rights Reserved.
 */
package com.leku.video.service.impl.video;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leku.commons.datasource.DataSourceContextHolder;
import com.leku.commons.util.CollectionUtil;
import com.leku.commons.util.GzipUtil;
import com.leku.commons.util.StringUtil;
import com.leku.video.constants.AppConstant;
import com.leku.video.constants.DataSourceConstant;
import com.leku.video.dao.video.VideoDao;
import com.leku.video.dto.request.video.VideoAddrReqDto;
import com.leku.video.dto.request.video.VideoReqDto;
import com.leku.video.dto.result.video.EpisodeResDto;
import com.leku.video.dto.result.video.VideoDto;
import com.leku.video.dto.result.video.VideoResDto;
import com.leku.video.dto.result.video.VideoUrlDto;
import com.leku.video.dto.result.video.VideoUrlResDto;
import com.leku.video.service.cache.MemberCache;
import com.leku.video.service.util.DealUrlUtil;
import com.leku.video.service.video.VideoService;

/**
 * 
 * 功能描述:视频服务
 *
 * @author Ranphy
 * @created 2018年10月24日
 * @version 1.0.0
 */
@Service
public class VideoServiceImpl implements VideoService {

	@Autowired
	private VideoDao videoDao;

	@Autowired
	private MemberCache memberCache;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.leku.video.service.video.VideoService#findVideoDetail(com.leku.video.
	 * dto.request.video.VideoReqDto)
	 */
	@Override
	public VideoResDto findVideoDetail(VideoReqDto reqDto) {
		// 设置数据源 ,影视数据库
		DataSourceContextHolder.setDataSourceType(DataSourceConstant.videoDataSource(reqDto.getAppCode()));

		VideoResDto result = new VideoResDto();

		VideoDto detail = new VideoDto();

		if (AppConstant.APP_YSDQ.equals(reqDto.getAppCode())) {
			detail = queryYSVideo(reqDto);
		} else {
			detail = queryRUVideo(reqDto);
		}

		if (StringUtil.isNotEmpty(reqDto.getUserId())) {
			Set<String> fvs = memberCache.findFavVideo(reqDto.getUserId(), reqDto.getAppCode());
			if (CollectionUtil.isNotEmpty(fvs) && fvs.contains(reqDto.getId())) {
				detail.setIscollection(true);
			}
		}

		result.setVideo(detail);

		return result;

	}

	/**
	 * 
	 * 功能描述:获取视频基本信息
	 *
	 * @param id
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private VideoDto queryRUVideo(VideoReqDto reqDto) {

		VideoDto video = videoDao.findVideoDetail(reqDto.getId());

		video.setIntro(GzipUtil.uncompress(video.getIntro()));
		if (video != null) {

			List<EpisodeResDto> episodes = videoDao.findEpisodes(reqDto.getId());

			for (EpisodeResDto episode : episodes) {
				episode.setTitle("第" + episode.getSeg() + "集");
			}

			video.setEpisodes(episodes);

		}
		return video;
	}

	/**
	 * 
	 * 功能描述:获取影视视频基本信息
	 *
	 * @param id
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private VideoDto queryYSVideo(VideoReqDto reqDto) {
		VideoDto video = videoDao.findYSVideoDetail(reqDto.getId());

		if (video != null) {
			List<EpisodeResDto> episodes = videoDao.findYSEpisodes(reqDto.getId(), video.getType());

			for (EpisodeResDto episode : episodes) {
				episode.setId(reqDto.getId() + "_" + episode.getSeg());
				episode.setTitle("第" + episode.getSeg() + "集");
				if (StringUtil.isEmpty(episode.getPic())) {
					episode.setPic(video.getPicH());
				}
			}

			video.setEpisodes(episodes);
		}
		return video;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.leku.video.service.video.VideoService#findVideoAddrs(com.leku.video.
	 * dto.request.video.VideoAddrReqDto)
	 */
	@Override
	public VideoUrlResDto findVideoAddrs(VideoAddrReqDto reqDto) {
		// 设置数据源 ,影视数据库
		DataSourceContextHolder.setDataSourceType(DataSourceConstant.videoDataSource(reqDto.getAppCode()));

		VideoUrlResDto result = new VideoUrlResDto();
		List<VideoUrlDto> urlList=null;
		
		if (AppConstant.APP_YSDQ.equals(reqDto.getAppCode())) {
			String[] ids = reqDto.getId().split("_");
			String lekuid = ids[0];
			String seg = ids[1];
			urlList=videoDao.findYSVideoAddrs(lekuid, seg);
			
		} else {
			urlList=videoDao.findVideoAddrs(reqDto.getId());
		}
		for (VideoUrlDto url : urlList) {

			// 处理地址
			url = DealUrlUtil.dealUrl(url);
		}

		result.setAddrs(urlList);

		return result;
	}

}
